/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.logging.*;
import javax.swing.*;

/**
 *
 * @author Lone_Wolf
 */
public class ModifierClient extends JPanel {

    private Fenetre fenetre;
    private Client client;
    Font f = new Font("Times New Roman", Font.BOLD, 24);
    private GridBagLayout gbl;
    private GridBagConstraints gbc;
    private JLabel lblNom;
    private JLabel lblCode;
    private JLabel lblTitre;
    private JLabel lblExisteDeja;
    private JTextField txtNom;
    private JTextField txtCode;
    private JButton btnModifier;
    private JButton btnAnnuler;
    private JButton btnSupprimer;

    public ModifierClient(Fenetre fenetre, Client client) {
        this.fenetre = fenetre;
        this.client = client;

        initComposantes();
        buildInterface();

        fenetre.setTitle("Modifier Client");
        //fenetre.setSize(400, 300);

        requestFirstFocus();
    }

    private void requestFirstFocus() {
        fenetre.addWindowFocusListener(new WindowAdapter() {

            public void windowGainedFocus(WindowEvent e) {
                txtNom.requestFocusInWindow();
            }
        });
        JFrame trick = new JFrame();
        trick.setVisible(true);
        trick.setVisible(false);
        fenetre.requestFocus();
    }

    private void initComposantes() {
        lblTitre = new JLabel("Modifier " + client.getNom());
        lblTitre.setFont(f);

        lblNom = new JLabel("Entrer le nom du client : ");
        lblCode = new JLabel("Entrer le code du client : ");
        lblExisteDeja = new JLabel("Ce client existe déjà!");
        lblExisteDeja.setForeground(Color.red);
        lblExisteDeja.setVisible(false);

        txtNom = new JTextField(client.getNom());
        txtCode = new JTextField(client.getCode());

        btnModifier = new JButton("Modifier");
        btnAnnuler = new JButton("Annuler");
        btnSupprimer = new JButton("Supprimer");

        btnModifier.addActionListener(new GererEvenements());
        btnAnnuler.addActionListener(new GererEvenements());
        btnSupprimer.addActionListener(new GererEvenements());

        txtNom.addKeyListener(new GererFocus());
        txtCode.addKeyListener(new GererFocus());
    }

    private void buildInterface() {
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        this.setLayout(gbl);
        gbc.fill = gbc.HORIZONTAL;

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;
        gbl.setConstraints(lblTitre, gbc);
        this.add(lblTitre);
        makeBorder();
        gbc.fill = gbc.NONE;
        gbc.anchor = gbc.LINE_END;
        gbl.setConstraints(btnSupprimer, gbc);
        //this.add(btnSupprimer);


        makeSeparator();
        makeSeparator();
        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                  //Bloc Nom
        gbc.fill = gbc.HORIZONTAL;
        gbl.setConstraints(lblNom, gbc);
        this.add(lblNom);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtNom, gbc);
        this.add(txtNom);

        gbc.gridwidth = gbc.REMAINDER;      //bords + Separateur
        makeBorder();
        makeSeparator();
        makeBorder();

        gbc.weightx = 0.1;                  //Bloc Code
        gbl.setConstraints(lblCode, gbc);
        this.add(lblCode);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtCode, gbc);
        this.add(txtCode);

        gbc.gridwidth = gbc.REMAINDER;      //bords
        makeBorder();
        makeSeparator();
        makeSeparator();

        makeBorder();                       //boutons
        gbc.weightx = 0.1;
        gbc.fill = gbc.NONE;
        gbl.setConstraints(btnAnnuler, gbc);
        gbl.setConstraints(btnModifier, gbc);
        this.add(btnAnnuler);
        makeBorder();
        gbl.setConstraints(btnModifier, gbc);
        this.add(btnModifier);
        makeBorder();
        makeSeparator();
        makeSeparator();

        makeBorder();
        makeBorder();
        makeBorder();
        gbc.weightx = 0.1;
        gbc.fill = gbc.NONE;
        gbl.setConstraints(lblExisteDeja, gbc);
        this.add(lblExisteDeja);
        makeBorder();
    }

    private void makeSeparator() {
        JLabel sep = new JLabel(" ");
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
        gbc.weighty = 0;
    }

    private void makeBorder() {
        gbc.weightx = 0.1;
        JLabel sep = new JLabel(" ");
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
    }

    private boolean validerDonnees() {
        boolean valide = false;
        btnModifier.setForeground(Color.darkGray);
        lblExisteDeja.setVisible(false);

        if (!txtNom.getText().isEmpty()) {
            lblNom.setForeground(Color.darkGray);
            if (!txtCode.getText().isEmpty()) {
                lblCode.setForeground(Color.darkGray);
                if (!alreadyExists()) {
                    valide = true;
                } else {
                    btnModifier.setForeground(Color.red);
                    lblExisteDeja.setVisible(true);
                }
            } else {
                lblCode.setForeground(Color.red);
                txtCode.requestFocusInWindow();
            }
        } else {
            lblNom.setForeground(Color.red);
            txtNom.requestFocusInWindow();
        }
        return valide;
    }

    private boolean alreadyExists() {
        boolean exists = false;
        Client clTemp;

        for (int i = 0; i < fenetre.arClient.size(); i++) {
            clTemp = fenetre.arClient.get(i);
            if (clTemp.getNom().equals(txtNom.getText()) || clTemp.getCode().equals(txtCode.getText())) {
                if (!clTemp.equals(client)) {
                    exists = true;
                }
            }
        }

        return exists;
    }

    class GererEvenements implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnModifier) {
                if (validerDonnees()) {
                    Client temp = client;
                    temp.setNom(txtNom.getText());
                    temp.setCode(txtCode.getText());

                    try {
                        fenetre.sauvegarderBD();
                        fenetre.AfficherEcranRecherche();

                    } catch (IOException ex) {
                        Logger.getLogger(CreerNouvClient.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(CreerNouvClient.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else if (e.getSource() == btnAnnuler) {
                try {
                    fenetre.AfficherEcranRecherche();
                } catch (IOException ex) {
                    Logger.getLogger(CreerNouvEmploye.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(CreerNouvEmploye.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (e.getSource() == btnSupprimer) {
                int reponse = JOptionPane.showConfirmDialog(null, "Êtes-vous certain de vouloir supprimer ce client?", "Confirmation", JOptionPane.YES_NO_OPTION);
                if (reponse == 0) {
                    fenetre.arClient.remove(client);
                    for (int i = 0; i < fenetre.arEntree.size(); i++) {
                        if (fenetre.arEntree.get(i).getClient().equals(client)) {
                            fenetre.arEntree.remove(i);
                        }
                    }
                    for (int i = 0; i < fenetre.arFactures.size(); i++){
                        if (fenetre.arFactures.get(i).getClient().equals(client)){
                            fenetre.arFactures.remove(i);
                        }
                    }
                    try {
                        fenetre.sauvegarderBD();
                        fenetre.AfficherEcranRecherche();

                    } catch (IOException ex) {
                        Logger.getLogger(CreerNouvClient.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(CreerNouvClient.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }

    class GererFocus implements KeyListener {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        /**
         * permet de changer le focus d'un champ de texte (JTextField) à un
         * autre grâce à la touche "Entrée".
         *
         * @param e permet d'itentifier la source dans laquelle une touche a
         * étée relâchée.
         */
        public void keyReleased(KeyEvent e) {
            if (e.getSource() == txtNom) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    txtCode.requestFocusInWindow();
                }
            } else if (e.getSource() == txtCode) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    btnModifier.doClick();
                }
            }
        }
    }
}
